package com.deer.wms.base.system.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.base.system.model.Supplier.SupplierManage;
import com.deer.wms.base.system.model.Supplier.SupplierManageCriteria;
import com.deer.wms.base.system.model.Supplier.SupplierManageDto;
import com.deer.wms.base.system.service.SupplierManageService;
import com.deer.wms.intercept.annotation.Authority;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.project.root.util.RedisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @author guo
 * @since 2019/11/21
 */
@Api(description = "供应商信息api接口")
@RestController
@RequestMapping("/supplier/manages")
public class SupplierManageController {

    @Autowired
    private SupplierManageService supplierManageService;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "新增供应商信息", notes = "新增供应商信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/insert")
    public Result add(@RequestBody SupplierManage supplierManage, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        List<SupplierManageDto> list = supplierManageService.findByCode(supplierManage.getSupplierCode());
        List<SupplierManageDto> list1 = supplierManageService.findByName(supplierManage.getSupplierName());
        //校验是否已经存在
        if (list1.size() != 0) {
            throw new ServiceException(CommonCode.SUPPLIER_EXISTS, "此供应商已存在");
        }
        if (list.size() != 0) {
            throw new ServiceException(CommonCode.SUPPLIER_CODE_EXISTS, "此供应商编码已存在");
        }

        String nowDate = DateUtils.getNowDateTimeString();
        supplierManage.setCreateTime(nowDate);
        supplierManageService.save(supplierManage);

        return ResultGenerator.genSuccessResult();
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Transactional
    @GetMapping("/delete")
    @ApiOperation(value = "删除供应商信息", notes = "删除供应商信息")
    public Result delete(String supplierCode, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        supplierManageService.deleteBySupplierCode(supplierCode);
        return ResultGenerator.genSuccessResult();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Transactional
    @PostMapping("/update")
    @ApiOperation(value = "更新供应商信息", notes = "更新供应商信息")
    public Result update(@RequestBody SupplierManage supplierManage, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        List<SupplierManageDto> list = supplierManageService.findByCode(supplierManage.getSupplierCode());
        if (list.size() == 0) {
            String nowDate = DateUtils.getNowDateTimeString();
            supplierManage.setCreateTime(nowDate);
            supplierManageService.updateById(supplierManage);
        } else if (list.size() == 1) {
            if (list.get(0).getSupplierId().equals(supplierManage.getSupplierId())) {
                List<SupplierManageDto> list1 = supplierManageService.findByName(supplierManage.getSupplierName());
                if (list1.size() == 0) {
                    String nowDate = DateUtils.getNowDateTimeString();
                    supplierManage.setCreateTime(nowDate);
                    supplierManageService.updateById(supplierManage);
                } else if (list1.size() == 1 && list1.get(0).getSupplierId().equals(supplierManage.getSupplierId())) {
                    String nowDate = DateUtils.getNowDateTimeString();
                    supplierManage.setCreateTime(nowDate);
                    supplierManageService.updateById(supplierManage);
                } else {
                    throw new ServiceException(CommonCode.SUPPLIER_EXISTS, "此供应商已存在");
                }

            } else if (!list.get(0).getSupplierId().equals(supplierManage.getSupplierId())) {

                throw new ServiceException(CommonCode.SUPPLIER_CODE_EXISTS, "此供应商编码已存在");
            }
        }

        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(Integer id) {
        SupplierManage supplierManage = supplierManageService.getById(id);
        return ResultGenerator.genSuccessResult(supplierManage);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Transactional
    @GetMapping("/list")
    public Result list(SupplierManageCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<SupplierManageDto> list = supplierManageService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Authority
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Transactional
    @GetMapping("/allList")
    public Result allList(@ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }

        List<SupplierManage> list = (List<SupplierManage>) redisUtil.get("allSupplierManage");
        if (list == null || list.size() == 0) {
             list =  supplierManageService.list();
            redisUtil.set("allSupplierManage", list,3600L);
        }

        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation(value = "往来单位查询")
    @PostMapping("/addSupplierManager")
    public Result addSupplierManager(@RequestBody Map<String,Object> hashMap){
        List<SupplierManage> list = supplierManageService.addSupplierManager(hashMap);
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation(value = "往来单位添加修改")
    @PostMapping("/saveSupplierManager")
    public Result saveSupplierManager(@RequestBody Map<String, Object>hashMap) {
        supplierManageService.saveSupplierManager(hashMap);
        return ResultGenerator.genSuccessResult();
    }
}
